#include "judge.h"

Judge::Judge()
{

}

bool judgeWin(ChessBoard b)
{
  std::vector<std::vector<Node>> block=b.getBoard();
  int same[8]={0};
  for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
          {
                //搜索五子形成
            /*向左搜索*/
            while (j >= 0 && block[i][j].getType() == BLACK)
            {
                    j--;
                    same[0]++;
            }
            /*左下搜索*/
            while (i < 15 && j >= 0 &&block[i][j].getType() == BLACK)
            {
                    i++;
                    j--;
                    same[1]++;
            }
            /*向下搜索*/
            while (i < 15 &&block[i][j].getType() == BLACK)
            {
                    i++;
                    same[2]++;
            }
            /*右下搜索*/
            while (i < 15 &&block[i][j].getType() == BLACK)
            {
                    i++;
                    j++;
                    same[3]++;
            }
            /*向右搜索*/
            while (i < 15 &&block[i][j].getType() == BLACK)
            {
                    j++;
                    same[4]++;
            }
            /*右上搜索*/
            while (i >= 0 && j < 15 &&block[i][j].getType() == BLACK)
            {
                    i--;
                    j++;
                    same[5]++;
            }
            /*向上搜索*/
            while (i >= 0 &&block[i][j].getType() == BLACK)
            {
                    i--;
                    same[6]++;
            }
            /*左上搜索*/
            while (i >= 0 && j >= 0 &&block[i][j].getType() == BLACK)
            {
                    i--;
                    j--;
                    same[7]++;
            }
            /*判断*/
            int l = 0;
            while (l < 4)
            {
                    if (same[l] + same[l + 4] == 4) //五子相连
                    {
                            return true;
                    }
                    l++;
            }
          }
    }
}
